const fetch = require('./fetch')
const sign = require('./commSign')
const sign2 = require('./getServiceSign')
const config = require('../config.js'); //获取应用实例


/**
 * 基础方法
 * URI 请求链接
 * params 请求参数
 */
function fetchApi(method, params) {
  let url = `${config.URI}/interface/${method}/v1` //请求url
  var params = sign.getCommonParameter(url, params) //请求参数
  return fetch.request(url, params)
}

/**
 * 发送 res.code 到后台换取 openId, sessionKey, unionId
 */

/**
 * 微信登录API
 */
function wxLogin(jsCode) {
  var params = {}
  params.jsCode = jsCode
  return fetchApi('user/getWechatInfoByCode', params)
}

/**
 * 补全用户信息
 */
function decryptUserInfo(userId, sessionKey, encryptedData, iv) { //userId, sessionKey, v, encryptedData
  var params = {}
  params.userId = userId
  params.sessionKey = sessionKey
  params.iv = iv
  params.encryptedData = encryptedData
  console.log(params)
  return fetchApi('user/decryptUserInfo', params)
}
/**
 * 获取查询标签列表API
 */
function findAllByCompanyId(params) {
  params.companyId = wx.getStorageSync('companyId')
  return fetchApi('store/findAllByCompanyId', params)
}
/**
 * 获取查询门店列表API
 */
function findAllByLabelIds(params) {
  params.companyId = wx.getStorageSync('companyId')
  return fetchApi('store/findAllByLabelIds', params)
}
/**
 * 获取腾讯位置服务导航路径API
 */
function getDirectionSign(params) {

  switch (params.direction) {
    case 'driving':
      params.direction = 'txDriving';break;
    case 'walking':
      params.direction = 'txWalking'; break;
    case 'bicycling':
      params.direction = 'txBicycling'; break;
    case 'transit':
      params.direction = 'txTansit'; break;
    default:
      params.direction = 'txDriving';
  }
  let path = config[params.direction];
  let url = config.txURI + sign2.getCommonParameter(path, params) //请求参数
  console.log(url)
  return new Promise((resolve, reject) => {
    wx.request({
      url: url,
      method: 'GET', //default: GET，value：OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      success: function(res) {
        resolve(res.data)
      },
      fail: reject
    })
  })
}
/**
 * 用户签到API
 */
function addSign(params) {
  return fetchApi('store/addSign', params)
}
/**
 * 用户注册API
 */
function register(params) {
  return fetchApi('user/register', params)
}
/**
 * 用户登录API
 */
function login(params) {
  return fetchApi('user/login', params)
}
/**
 * 用户注册前获取企业列表信息API
 */
function getCompanys(params) {
  return fetchApi('user/getCompanys', params)
}
/**
 * 多文件上传基础方法
 */
function uploadFileApi(params, tempFilePaths) {
  let url = `${config.URI}/wxUploadFiles` //请求url
  var params = sign.getCommonParameter(url, params) //请求参数
  return fetch.uploadFiles(url, params, tempFilePaths, 0, 0, 0, tempFilePaths.length)
}
module.exports = {
  findAllByCompanyId,
  findAllByLabelIds,
  getDirectionSign,
  addSign,
  uploadFileApi,
  wxLogin,
  decryptUserInfo,
  login,
  register,
  getCompanys
}